﻿using DataTransfer.Core.Models;
using DataTransfer.Core.Services;
using Microsoft.AspNetCore.Mvc;
using MiniFox.Platform;
using MiniFox.Web.Filters;
using MiniFox.Web.Infrastructure;
using MiniFox.Platform.Applications.Web;
using DataTransfer.Core;
using MiniFox.Infrastructure;
using MiniFox.Platform.File.Web;
using MiniFox.Utility;
using DataTransfer.SDK.Models;
using Org.BouncyCastle.Asn1.Ocsp;
using MiniFox.Platform.File.Models;

namespace DataTransfer.WebApi.Controllers
{
    /// <summary>
    /// 数据导入API
    /// </summary>
    [NoCache, ValidationFilter, Area(GlobalConstants.AREA_NAME)]
    public class ImportController : BinaryControllerBase
    {
        protected virtual ImportEngine CreateComponentByName(string name, string importerName)
        {
            return this.Container.GetComponentByName<ImportEngine>(name, importerName, HttpContext.GetAuthState());
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <param name="importer"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Response<ImportBatchResult>> Upload([FromRoute(Name = GlobalKey.NAME)] string name, [FromQuery] string importer)
        {
            var fileDatas = await this.Request.HttpFileBatchLoadAsync();
            var service = this.CreateComponentByName(name, importer);
            var result = service.Execute(fileDatas);
            return result.CreateResponse();
        }
        /// <summary>
        /// 预览文件数据
        /// </summary>
        /// <param name="name"></param>
        /// <param name="importer"></param>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public Response<object> Preview([FromRoute(Name = GlobalKey.NAME)] string name, [FromQuery] string importer, [FromBody] PagedQueryObject<PreviewRequest> request)
        {
            var service = this.CreateComponentByName(name, importer);
            var result = service.Preview(request);
            return result.CreateResponse();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <param name="importer"></param>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public Response<object> Remove([FromRoute(Name = GlobalKey.NAME)] string name, [FromQuery] string importer, [FromBody] RemoveRequest request)
        {
            var service = this.CreateComponentByName(name, importer);
            var obj = service.Remove(request);
            return obj.CreateResponse();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <param name="importer"></param>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public Response<object> Update([FromRoute(Name = GlobalKey.NAME)] string name, [FromQuery] string importer, [FromBody] UpdateRequest request)
        {
            var service = this.CreateComponentByName(name, importer);
            var obj = service.Update(request);
            return obj.CreateResponse();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <param name="importer"></param>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public Response<object> Undo([FromRoute(Name = GlobalKey.NAME)] string name, [FromQuery] string importer, [FromBody] UndoRequest request)
        {
            var service = this.CreateComponentByName(name, importer);
            var obj = service.Undo(request);
            return obj.CreateResponse();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <param name="importer"></param>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public Response UndoAll([FromRoute(Name = GlobalKey.NAME)] string name, [FromQuery] string importer, [FromBody] UndoAllRequest request)
        {
            var service = this.CreateComponentByName(name, importer);
            service.UndoAll(request);
            return new();
        }
        /// <summary>
        /// 确认导入，将预加载的数据导入到目标，并在导入后清除缓存数据
        /// </summary>
        /// <param name="name"></param>
        /// <param name="importer"></param>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public Response<ImportBatchResult> Accept([FromRoute(Name = GlobalKey.NAME)] string name, [FromQuery] string importer, [FromBody] ImportAcceptance request)
        {
            var service = this.CreateComponentByName(name, importer);
            var result = service.Accept(request);
            return result.CreateResponse();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <param name="importer"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult Template([FromRoute(Name = GlobalKey.NAME)] string name, [FromQuery] string importer)
        {
            var component = this.CreateComponentByName(name, importer);
            FileData fileData = component.GetTemplate();

            return this.Execute(fileData.Data, fileData.ContentType, fileData.FileName);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <param name="importer"></param>
        /// <param name="batchId"></param>
        /// <param name="fileId"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult Load([FromRoute(Name = GlobalKey.NAME)] string name, [FromQuery] string importer, [FromQuery] string batchId, [FromQuery] string fileId)
        {
            var service = this.CreateComponentByName(name, importer);
            FileData fileData = service.GetFile(batchId, fileId);
            return Execute(fileData.Data, fileData.ContentType, fileData.FileName);
        }
    }
}